<!DOCTYPE html>
<html>
<head>
<head/>
<div th:include="base-libs :: base-libs" />
<div th:include="common/yljg-modal :: yljg-modal" />
<div th:include="common/common-export :: common-export" />
<title>服务包管理</title>
<style type="text/css">
</style>
</head>
<script th:inline="javascript">
	/**用户信息*/
	var userInfo = [[${userInfo}]];
</script>
<script type="text/javascript">
$(function() {
	enterToQuery(queryForm);
 	//initView();
    $(this).registerManagerPage({cacheTables:["TB_ZY_QYDX","TB_ZY_FWBLX"],callback:initView});
 	//$("#btnQueryFwb").trigger("click");
});

function queryForm() {
	$("#fwbForm").datagrid("reload");
	/**禁用更新按钮*/
	if($("#btnUpdate").attr("disabled")!="disabled"){
		//$("#btnUpdate").attr("disabled", "disabled");
	}
	/**清空勾选*/
	selectionIds = [];
}
function queryHandler(result) {
	$("#fwbForm").bootstrapTable("load", result);
}

function initView(cacheData)
{
	var opt ={
			idField : "FWBID",
			uniqueId: "FWBID",
			striped : true,
			border:false,
			toolbar : "#dgFormToolbar",
			columns : [[
				//{field: "checkStatus",checkbox: true},
				{field : "FWBID",width: 170,align: "center",title : "服务包编码"},
				{field : "FWBMC",width: 160,align: "center",title : "服务包名称"},
				{field : "YLJGMC",width: 180,align: "center",title : "所属机构"},
                {field : "FWBLX",width: 100,align: "center",title : "服务包类型",formatter:function(value){
                    var TB_ZY_FWBLX = cacheData.TB_ZY_FWBLX;
                    $.each(TB_ZY_FWBLX,function(){
                        if(this.XMDM==value){
                            value = this.XMMC;
                            return false;
                        }
                    })
                    return value;
				}},
                {field : "QYDX",width: 100,align: "center",title : "签约对象",formatter:function(value){
			    	var TB_ZY_QYDX = cacheData.TB_ZY_QYDX;
					$.each(TB_ZY_QYDX,function(){
					    if(this.XMDM==value){
							value = this.XMMC;
							return false;
						}
					})
					return value;
				}},
				{field : "FY",width: 100,align: "center",title : "优惠后价格",
					formatter:function(value){
						return value + "元"
					}
				},
                {field : "SFBHYBFWX",width: 80,align: "center",title : "是否包含<br/>医保服务项",hidden:true,formatter:function(value){
			    	if(value==1) return "是"; else return "否";
				}},
				{field : "FWBZT",width: 60,align: "center",title : "状态",
					formatter : function(value, row, index){
						return getFwzt(value);
					}
				},
				{field : "CJSJ",width: 90,align: "center",title : "创建日期",
					formatter : function(value, row, index){
						if(!StringUtils.isEmpty(value)){
							value = value.substr(0, 10);
						}
						return value;
					}
				},
				{field : "cz",width: 100,align: "center",title : "操作",formatter : operateFormatter}
			]]
			,pagination : true
			,pageNumber: 1
			,pageSize: 10
			,fit: true
			,fitColumns: true
			,singleSelect: true
			,rownumbers: true
			,method : "post"
			,url: contextRoot+"fwb/queryPage.service"
			,onBeforeLoad:function(params){
			    /* 查询要求：管理机构(R开头):可查询其上下级；非管理机构：查询上级和本机构 */
				params["FWBMC"] = $("#FWBMC").val();
				var YLJGMC = $("#YLJGMC").val();
				if(YLJGMC==lastSelectYljgmc){
					params["YLJGID"] = $("#YLJGID").val();
				}else{
                    params["YLJGMC"] = YLJGMC;
				}
				params["FWBZT"] = $("#FWBZT").val();
				params["XZQHDM"] = userInfo["JGXZQHDM"];
				params["JGLB"] = parent.userInfo.WSJGLBDM;
				/**判断是否为机构管理员进入*/
				/* if(userInfo["YLJGDM"] != "000000"){
					params["YLJGID"] = userInfo["PTJGID"];
				} */
				return params;
			},
			onLoadSuccess:function(){
			    if(NFTJ_YBFWX){//能否添加医保服务项
					$(this).datagrid("showColumn","SFBHYBFWX");
				}
			}
		};
	$("#fwbForm").datagrid(opt);
	
	/**禁用修改按钮*/
	//$("#btnUpdate").attr("disabled", "disabled");
	
	/**加载选择方法*/
	SelectCheckBox("FWBID","fwbForm", "btnUpdate", opt);
	
	/**更新服务项*/
	$("#btnUpdate").on("click", function(){
		var info = $("#fwbForm").datagrid("getSelections");
		if(info.length>0){
			if(info.length == 1){
				info = info[0];
				/**判断启用服务包不可修改*/
				if(info["FWBZT"] == "0"){
					StoreCache.setCache("FWB_INFO", info);
					$.QuickUrlUtils.redirectUrl("manager.html?FWBID="+info["FWBID"]);
				}else{
					$.QuickAlert.alertFail({content : "启用服务包不可修改，请先停用服务包！"});
				}
			}else{
				$.QuickAlert.alertFail({content : "请选中单个服务包！"});
			}
		}else{
			$.QuickAlert.alertFail({content : "请选中服务包！"});
		}
	});
	
	/**查询服务包*/
	$("#btnQueryFwb").on("click", function(){
		queryForm();
	});
	
	/**新建服务包*/
	$("#btnAdd").on("click", function(){
		$.QuickUrlUtils.redirectUrl("manager.html");
	});
	
	/**导出服务包*/
	$("#btnExport").on("click", function(){
		$("#JGXZQHDM").val(userInfo.JGXZQHDM);
		$("#exportForm").submit();
	});

	var lastSelectYljgmc; //查询条件选择的医疗机构名称
	$("#xzYljg").on("click", function(){
	    StoreCache.setCache("isFwbQueryCondition",true);
		//页面层
		parent.layer.open({
		  type: 1,
		  area: ["80%", "500px"], //宽高   
		  scrollbar: false,
		  resize: false,
		  content: $("#yljgModal").html(),
		  move: false,
		  title: '选择机构',
		  btn: ["确定", "取消"],
		  success: function(layero, index){
			  initYljgTable(layero);
		  },
		  yes: function (index, layero) {
          			var info = $(layero).find("#yljgForm").datagrid("getSelected")
			  		if(!$.isEmptyObject(info)){
						$("#YLJGMC").val(info["YLJGMC"]);
						$("#YLJGID").val(info["PTJGID"]);
                        lastSelectYljgmc = info["YLJGMC"];
					}else{
                        $("#YLJGMC,#YLJGID").val(""); //clear
					}
         			parent.layer.close(index);//这块是点击确定关闭这个弹出层
          }
		});
	});
	
}

/**操作格式化*/
function operateFormatter(value, row, index) {
	if(userInfo["PTJGID"] != row["XJJGID"] && (row["JGLB"]).substr(0,1) == "R"){
		return ($("#tableToolBar").html()).replace(/#index#/g,index);
	}
	var tool = $("#tableToolBarStop").html();
	if(row["FWBZT"] == "0"){
		tool = $("#tableToolBarStart").html();
	}
	return tool.replace(/#index#/g,index);;
}

/**详细按钮*/
function detail(index){
	/**清除所有选择*/
	$("#fwbForm").datagrid("clearSelections");
	/**选中当前行*/
	$("#fwbForm").datagrid("selectRow", index);
	/**获取选中行数据*/
	var row = $("#fwbForm").datagrid("getSelected", index);
	StoreCache.setCache("FWB_INFO", row);
	$.QuickUrlUtils.redirectUrl("detail.html?FWBID="+row["FWBID"]);
}
/**启用按钮*/
function start(index){
	/**清除所有选择*/
	$("#fwbForm").datagrid("clearSelections");
	/**选中当前行*/
	$("#fwbForm").datagrid("selectRow", index);
	/**获取选中行数据*/
	var row = $("#fwbForm").datagrid("getSelected", index);
	$.QuickAlert.alertConfirm({content: "确认是否启用服务包？", callback: function(){
		$("#startForm").find(".FWBID").val(row["FWBID"]);
		$("#startForm").find(".FWBZT").val("1");
		$.QuickRemote.AjaxFiledFormSubmit("startForm", function(result) { 
			var info = result["info"];
			if (result["state"]) {
				$.QuickAlert.alertNormal("启用成功！",{callback: function(){
					$("#btnQueryFwb").trigger("click");
				}});
			} else {
				$.QuickAlert.alertFail({
					content : info["faultInfo"]
				});
			}
		});
	}});
}
/**停用按钮*/
function stop(index){
	/**清除所有选择*/
	$("#fwbForm").datagrid("clearSelections");
	/**选中当前行*/
	$("#fwbForm").datagrid("selectRow", index);
	/**获取选中行数据*/
	var row = $("#fwbForm").datagrid("getSelected", index);
	$.QuickAlert.alertConfirm({content: "确认是否停用服务包？", callback: function(){
		$("#stopForm").find(".FWBID").val(row["FWBID"]);
		$("#stopForm").find(".FWBZT").val("0");
		$.QuickRemote.AjaxFiledFormSubmit("stopForm", function(result) { 
			var info = result["info"];
			if (result["state"]) {
				$.QuickAlert.alertNormal("停用成功！",{callback: function(){
					$("#btnQueryFwb").trigger("click");
				}});
			} else {
				$.QuickAlert.alertFail({
					content : info["faultInfo"]
				});
			}
		});
	}});
}

/**获取服务状态*/
function getFwzt(fwbzt){
	if(fwbzt == "0"){
		fwbzt = "停用";
	}
	else if(fwbzt == "1"){
		fwbzt = "启用";
	}
	return fwbzt;
}

/**获取有效期*/
function getYxq(yxq){
	if(yxq == "01"){
		yxq = "一个月";
	}
	else if(yxq == "02"){
		yxq = "三个月";
	}
	else if(yxq == "03"){
		yxq = "半年";
	}
	else if(yxq == "04"){
		yxq = "一年";
	}
	return yxq;
}
</script>

<body class="easyui-layout" >

<div region="north" style="border: none; overflow: hidden;">
		<h4 class="h4-title" style="text-align:center;margin:0;padding:0;font-size: 18px;">
			服务包管理
		</h4>
	</div>
	<div region="center" style="border: none;">
		<div id="dgFormToolbar" style="background: white;">
			<form id="startForm" action="start">
				<div style="display: none;">
					<input class="FWBID"  name="FWBID" />
					<input class="FWBZT"  name="FWBZT" />
				</div>
			</form>
			<form id="stopForm" action="stop">
				<div style="display: none;">
					<input class="FWBID"  name="FWBID" />
					<input class="FWBZT"  name="FWBZT" />
				</div>
			</form>
			
			<!-- 导出form -->
			<form id="exportForm" action="export.service" enctype="multipart/form-data">
				<table class="form-table" style="margin:0;padding:0;">
					<tr>
						<td class="td-text">服务包名称：</td>
						<td class="td-content">
							<input type="hidden" name="XZQHDM" id="JGXZQHDM" />
							
							<input id="FWBMC" name="FWBMC" class="form-control" type="text"
								placeholder="服务包名称">
						</td>
						<td class="td-text">所属机构：</td>
						<td class="td-content">
							<div style="display: inline;">
								<input id="YLJGMC" class="form-control dataField" style="width: 55%;"/>
								<input type="hidden" id="YLJGID" name="YLJGID">
								<!-- 按钮触发模态框 -->
								<button class="btn" type="button"  style="width: 36%;"  id="xzYljg">选择</button>
							</div>
						</td>
						<td class="td-text">状态：</td>
						<td class="td-content">
							<select id="FWBZT" name="FWBZT" class="form-control">
								<option value="">请选择</option>
								<option value="0">停用</option>
								<option value="1">启用</option>
							</select>
						</td>
						<td class="td-text" style="text-align:center;width:180px;">
							<button id="btnQuery" class="btn btn-info" type="button" id="btnQueryFwb">查询</button>
							<button id="btnExport" class="btn btn-info" type="button">导出</button>
						</td>
					</tr>
				</table>
			</form>
			<div style="margin:10px 10px auto 0px;">
				<button id="btnAdd" class="btn btn-info" type="button">新建服务包</button>
				<button id="btnUpdate" class="btn btn-info" type="button">修改服务包</button>
			</div>
		</div>
		<div class="easyui-layout" style="border: none;" fit="true">
			<div region="center" style="border: none;">
				<table id="fwbForm">
				</table>
			</div>
		</div>
	</div>
</body>
<script type="text/html" id="tableToolBar">
<a class="detail tableToolBarBtn" href="javascript:detail(#index#)" title="详细" >详细</a>
</script>
<script type="text/html" id="tableToolBarStart">
<a class="detail tableToolBarBtn" href="javascript:detail(#index#)" title="详细" >详细</a>
<a class="start tableToolBarBtn" href="javascript:void(0)" title="启用" onclick="start(#index#)">启用</a>
</script>
<script type="text/html" id="tableToolBarStop">
<a class="detail tableToolBarBtn" href="javascript:detail(#index#)" title="详细">详细</a>
<a class="stop tableToolBarBtn" href="javascript:void(0)" title="停用" onclick="stop(#index#)">停用</a>
</script>
</html>



